<HTML><HEAD><TITLE>intersection(+List1, +List2, ?Common)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">library(lists)</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>intersection(+List1, +List2, ?Common)</H1>
Succeeds if Common unifies with the list which contains the common elements
of List1 and List2.


<DL>
<DT><EM>+List1</EM></DT>
<DD>List.
</DD>
<DT><EM>+List2</EM></DT>
<DD>List.
</DD>
<DT><EM>?Common</EM></DT>
<DD>List or variable.
</DD>
</DL>
<H2>Description</H2>
   Common is unified with a list which contains the common elements of
   List1 and List2.
<P>
   The definition of this Prolog library predicate is:
<PRE>
intersection([], _, []).
intersection([Head|L1tail], L2, L3) :-
        memberchk(Head, L2),
        !,
        L3 = [Head|L3tail],
        intersection(L1tail, L2, L3tail).
intersection([_|L1tail], L2, L3) :-
        intersection(L1tail, L2, L3).
</PRE>
   This predicate does not perform any type testing functions.
<P>
   This predicate works properly for set operations only, so repeated
   elements and variable elements should not be used in the lists.
	
<H3>Modes and Determinism</H3><UL>
<LI>intersection(+, +, -) is det
</UL>
<H3>Fail Conditions</H3>
   Fails if Common does not unify with the list which contains the common
   elements of List1 and List2.


<H3>Resatisfiable</H3>
   Yes.
<H2>Examples</H2>
<PRE>
Success:
   intersection([1,2],[2,3],L).     (gives L=[2]).
   intersection([a,d],[a,b,c],[a]).

Fail:
   intersection([a,b],[a,b],[b]).


</PRE>
<H2>See Also</H2>
<A HREF="../../lib/lists/subtract-3.html">subtract / 3</A>, <A HREF="../../lib/lists/memberchk-2.html">memberchk / 2</A>, <A HREF="../../lib/lists/union-3.html">union / 3</A>
</BODY></HTML>
